// pages/menu/menu.js
import { request } from "../request/index"
import regeneratorRuntime from "../request/runtime"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Leftcate: [],
    Rightcate: [],
    //被点击的左侧菜单
    currentIndex: 0,
    scrolltop: 0
  },
  //返回接口的数据
  catelist: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const catelist = wx.getStorageSync('cates');
    if (!catelist) {
      this.getcategoriesList();
    } else {
      if (Date.now() - catelist.time > 1000 * 10) {
        this.getcategoriesList();
      } else {
        this.catelist = catelist.data;
        let Leftcate = this.catelist.map(v => v.cat_name);
        let Rightcate = this.catelist[0].children;
        this.setData({
          Leftcate,
          Rightcate
        })
      }
    }
  },
  async getcategoriesList() {
    const res = await request({
      url: "/categories"
    });
    this.catelist = res;
    wx.setStorageSync('cates', { time: Date.now(), data: this.catelist })
    let Leftcate = this.catelist.map(v => v.cat_name);
    let Rightcate = this.catelist[0].children;
    this.setData({
      Leftcate,
      Rightcate
    })
  },
  LeftTap(e) {
    const { index } = e.currentTarget.dataset;
    let Rightcate = this.catelist[index].children;
    this.setData({
      currentIndex: index,
      Rightcate,
      scrolltop: 0
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})